/*  Menu engine
*
*/
#include <allegro.h>
/*
*
*/
#include "consts.h"
#include "menu.h"

engineStruct menu() {
  BITMAP * buffer = create_bitmap(GAME_W, GAME_H);
  
  int mode = MODE_GAME;
  
  // Position of the cursor
  int x,y;  
  int charCursorX[3] = { 40, 230, 420 } ;
  int currentCursor;
  int keypressed = 0;
  // <dev>
  /*
  * Character Selection
  */
    BITMAP * charSelect_title = load_bitmap("images/gui/charSelect/item_title.bmp", NULL);
    BITMAP * charSelect_quit = load_bitmap("images/gui/charSelect/item_quit.bmp", NULL);
    BITMAP * charSelect_char1 = load_bitmap("images/gui/charSelect/item_noe.bmp", NULL);
    BITMAP * charSelect_char3 = load_bitmap("images/gui/charSelect/item_nico.bmp", NULL);
    BITMAP * charSelect_char4 = load_bitmap("images/gui/charSelect/item_locked.bmp", NULL);
    BITMAP * charSelect_cursor = load_bitmap("images/gui/charSelect/item_selector.bmp", NULL);
    currentCursor = 0;
    int character = 0;
    x = charCursorX[currentCursor];
    y = 232;
    
  // </dev>
  rest(100);
  while(1) {
    if ( key[KEY_DOWN]) {
      x = 270;
      y = 400;
      mode = MODE_QUIT;
      currentCursor = 3;
    }
    if ( key[KEY_UP] ) {
      mode = MODE_GAME;
      currentCursor = 0;
      x = charCursorX[currentCursor];
      y = 232;
    }
    if ( key[KEY_RIGHT] && currentCursor != 3 && keypressed == 0) {
     ++currentCursor;
     if ( currentCursor == 3 ) currentCursor = 0;
      x = charCursorX[currentCursor];
      y = 232;
      keypressed = 1;
      character = currentCursor;
    }
    if ( key[KEY_LEFT] && currentCursor != 3 && keypressed == 0) {
     --currentCursor;
     if ( currentCursor == -1 ) currentCursor = 2;
      x = charCursorX[currentCursor];
      y = 232;
      keypressed = 1;
      character = currentCursor;      
    }
    if ( !key[KEY_RIGHT] && !key[KEY_LEFT] ) keypressed = 0;
    if ( key[KEY_ENTER] ) break;
    
    draw_sprite(buffer, charSelect_title, 88, 10);
    draw_sprite(buffer, charSelect_quit, 295, 400);    
    draw_sprite(buffer, charSelect_char1, 67, 117);
    draw_sprite(buffer, charSelect_char4, 257, 117);
    draw_sprite(buffer, charSelect_char3, 447, 117);
    draw_sprite(buffer, charSelect_cursor, x, y);   
    blit(buffer, screen, 0, 0, 0, 0, GAME_W, GAME_H);
    clear_bitmap(buffer);
    
  }
  destroy_bitmap(charSelect_title);
  destroy_bitmap(charSelect_quit);
  destroy_bitmap(charSelect_char1);
  destroy_bitmap(charSelect_char3);
  destroy_bitmap(charSelect_char4);    
  destroy_bitmap(charSelect_cursor);  
  destroy_bitmap(buffer);
  
  engineStruct comm;
  comm.mode = mode;
  comm.data = character;
  return comm;
}
